﻿using UnityEngine;
using System.Collections;
using GhostMoba.Utilities;
using LitJson;
using System.Text;
using System.Collections;
using System.Security.Cryptography;
using System;

public class people
{
   public  string name;
    public int age;
    public people(string name,int age)
    {
        this.name = name;
        this.age = age;
    }
}

public class test : MonoBehaviour {

    private string key = "12345678912345678912345678912345";

    // Use this for initialization
    void Start() 
    {
        JSONClass jd = new JSONClass();
        jd["name"] = "caojianlin";
        jd["info"] = new JSONClass();
        jd["info"]["age"] = "29";
        jd["info"]["contry"] = "chian";

        string jsStr = jd.ToString();
        Debug.LogError("========11=======" + Encrypt(jsStr));

        jsStr = Encrypt(jsStr);
        Debug.LogError("========22=======" + jsStr);

        JSONNode ad = new JSONNode();
        ad = JSON.Parse(Decrypt(jsStr));

        Debug.LogError("++++++++++++" + ad["name"] + "+++++++" + ad["info"]["age"]);
        Debug.LogError("---------------" + ad.ToString());

    }

    //字符串加密
    private string Encrypt(string toE)
    {
        byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key);

        RijndaelManaged rDel = new RijndaelManaged();
        rDel.Key = keyArray;
        rDel.Mode = CipherMode.ECB;
        rDel.Padding = PaddingMode.PKCS7;
        ICryptoTransform ctranform = rDel.CreateEncryptor();

        byte[] toEncrytArray = UTF8Encoding.UTF8.GetBytes(toE);
        byte[] resultArray = ctranform.TransformFinalBlock(toEncrytArray, 0, toEncrytArray.Length);

        return Convert.ToBase64String(resultArray, 0, resultArray.Length);

    }

    //字符串解密
    private string Decrypt(string toD)
    {
        byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key);

        RijndaelManaged rDel = new RijndaelManaged();
        rDel.Key = keyArray;
        rDel.Mode = CipherMode.ECB;
        rDel.Padding = PaddingMode.PKCS7;
        ICryptoTransform ctranform = rDel.CreateDecryptor();

        byte[] toEncrypArray = Convert.FromBase64String(toD);
        byte[] resultArray = ctranform.TransformFinalBlock(toEncrypArray, 0, toEncrypArray.Length);

        return UTF8Encoding.UTF8.GetString(resultArray);
    }

	
}
